// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;
 
import constants from './constants';
import BallGroup from './ball_group';
import BombGroup from './bomb_group';

 

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Integer)
    fireSpeed: number = 50;

 
 
 
 

    @property(cc.Node)
    ballSprite:cc.Node=null

    rgBd:cc.RigidBody=null

    startPos:cc.Vec2=null;

    @property(cc.AudioClip)
    fireAudioClip:cc.AudioClip=null;

      @property(cc.AudioClip)
      mergeAudioClip:cc.AudioClip=null;
 
      @property({type:BallGroup})
      ballGroup:BallGroup=null;

 
  
 

    // LIFE-CYCLE CALLBACKS:

     onLoad () {
        this.rgBd = this.getComponent(cc.RigidBody);
        this.ballGroup=this.node.parent.getComponent(BallGroup);
     }

     init(){
         
     }

     update(){
        var minX = -this.node.parent.width/2+this.node.width/2;
        var maxX = -minX;
        var minY = -this.node.parent.height/2+this.node.height/2;
        var maxY = -minY;
        if (this.node.x< minX||this.node.x>maxX||this.node.y<minY||this.node.y>maxY){
            this.ballGroup.died(this.node);
        }
        // console.log(this.node.getPosition());
        if(this.startPos&&this.rgBd.linearVelocity.mag()<80&&this.node.getPosition().y>-400){
             let bombNode= this.ballGroup.bombGroup.getNewBomb();
            bombNode.setPosition(this.node.getPosition());
            bombNode.getComponent(cc.ParticleSystem).resetSystem();
            // console.log(bombNode,bombNode.getComponent(cc.ParticleSystem));
            // bombNode.getComponent(cc.ParticleSystem).play();
             this.ballGroup.died(this.node);
        }
   
     }

    start () {

    }
    fire(angle,speed){
        this.startPos=this.node.getPosition(); 
        let speedValue;
        if(speed){
           speedValue=speed;
        }
        else{
            speedValue=100*this.fireSpeed;
        }
 
        var impulse= cc.pRotateByAngle(cc.p(0,speedValue),cc.p(0,0),-cc.degreesToRadians(angle));
        this.rgBd.applyLinearImpulse(impulse,this.node.convertToWorldSpaceAR(cc.p(0, 0)), true);
        cc.audioEngine.play(this.fireAudioClip as any,false,1);
    }
    
}
